<?php


namespace app\shop\service\until;


use OSS\OssClient;
use think\facade\Config;

class OssService
{
    private $ossConfig;

    public function __construct()
    {
        $disk = env('filesystem.driver', 'local');
        $this->ossConfig = Config::get('filesystem.disks.' . $disk);
    }

    public function uploadFile(string $object, string $filePath)
    {
        $accessKeyId = $this->ossConfig['accessId'];
        $accessKeySecret = $this->ossConfig['accessSecret'];
        $endpoint = $this->ossConfig['endpoint'];
        $bucket = $this->ossConfig['bucket'];
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $result = $ossClient->uploadFile($bucket, $object, $filePath);
            return $result;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 判断文件是否存在
     * @param $filePath '/image/85/2024-07-31/66a9d7c1655a6.jpg';
     * @return bool
     * @throws \Exception
     */
    public function existFile(string $filePath): bool
    {
        if ($filePath[0] === '/') {
            $filePath = substr($filePath, 1);
        }
        $accessKeyId = $this->ossConfig['accessId'];
        $accessKeySecret = $this->ossConfig['accessSecret'];
        $endpoint = $this->ossConfig['endpoint'];
        $bucket = $this->ossConfig['bucket'];
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            return $ossClient->doesObjectExist($bucket, $filePath);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除文件
     * @param string $filePath
     * @return bool
     * @throws \Exception
     */
    public function delFile(string $filePath): bool
    {
        if ($filePath[0] === '/') {
            $filePath = substr($filePath, 1);
        }
        $accessKeyId = $this->ossConfig['accessId'];
        $accessKeySecret = $this->ossConfig['accessSecret'];
        $endpoint = $this->ossConfig['endpoint'];
        $bucket = $this->ossConfig['bucket'];
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ossClient->deleteObject($bucket, $filePath);
            return true;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     *获取临时上传路径
     * @param string $object
     * @return string
     * @throws \Exception
     */
    public function signUrl(string $object)
    {
        $accessKeyId = $this->ossConfig['accessId'];
        $accessKeySecret = $this->ossConfig['accessSecret'];
        $endpoint = $this->ossConfig['endpoint'];
        $bucket = $this->ossConfig['bucket'];
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $result = $ossClient->signUrl($bucket, $object, 600, "PUT", array('Content-Type' => 'image/png'));
            return $result;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }
}